﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using LevenBlog.Core.Entities;
using LevenBlog.Core.User;

namespace LevenBlog.Core.Mvc
{
    public sealed class ManagerAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
    {
        private bool isManager = true;
        private string role;

        public bool IsManager
        {
            get { return isManager; }
            set { isManager = false; }
        }

        public string Role
        {
            get { return role; }
            set { role = value; }
        }

        public ManagerAuthorizeAttribute()
        {
            this.role = string.Empty;
        }

        public ManagerAuthorizeAttribute(string role)
        {
            this.role = role;
        }

        public ManagerAuthorizeAttribute(string role, bool isManager)
        {
            this.role = role;
            this.isManager = isManager;
        }

        private bool CheckRole(IAuthority authory)
        {
            string roleString = authory.ToCode();
            string[] ts = roleString.Split('|');
            var result = from t in ts
                         where t.Trim().Split('=')[0] == role
                         select t.Split('=')[1];
            if (result.Count()>0)
            {
                if (result.Single() == "True")
                    return true;
                else
                    return false;
            }
            else
            {
                return false;
            }
        }

        public void OnAuthorization(AuthorizationContext filterContext)
        {
            AdminMembership adminMembership = (filterContext.Controller as AdminBaseController).Membership as AdminMembership;
            if (this.isManager)
            {
                if (!(adminMembership.Authority as Authority).AdminLink)
                {
                    //filterContext.Cancel = true;
                    filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { controller = "blog", action = "index", page = "1" }));
                }
            }
            if (!string.IsNullOrEmpty(role))
            {
                if (!CheckRole(adminMembership.Authority))
                {
                    //filterContext.Cancel = true;
                    MessageEntity message = new MessageEntity();
                    message.Title = "权限错误";
                    message.Content = "对不起,您无权进行该操作,请返回继续,如有疑问,请联系系统管理员.";
                    message.JumpUrl = "<a href=\"javascript:history.back();\" title=\"返回\">返回</a>";
                    message.Icon = MessageIcon.Error;
                    (filterContext.Controller as Controller).ViewData.Model = message;
                    ViewResult view = new ViewResult();
                    view.ViewData = (filterContext.Controller as Controller).ViewData;
                    view.ViewName = "message";
                    //view.ViewEngine = (filterContext.Controller as Controller).ViewEngine;
                    filterContext.Result = view;
                }
            }
        }
    }
}
